package com.yjk.dzpoke.websocket;

import com.yjk.dzpoke.websocket.bean.WsMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@EnableAsync
@RestController
@EnableScheduling
public class HeartBeat {
    @Scheduled(cron = "*/30 * * * * ?")
    public void heartBeat() throws IOException {
        ConcurrentHashMap<String, PlayServer> map = PlayServer.getWebSocketMap();
        if (map.size() != 0) {
            for (Map.Entry<String, PlayServer> entry : map.entrySet()) {
                PlayServer server = entry.getValue();
                server.sendMessage(Arrays.asList(
                        WsMsg.heartBeat()
                ).toString());
            }
        }
    }
}
